//
//  LJHRecommendViewController.m
//  HEARME
//
//  Created by liujinhui on 16/1/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LJHRecommendViewController.h"
#import "LJHAppTools.h"
#import "LJHSectionModel.h"
#import "LJHRecScrollView.h"
#import "LJHBaseTableView.h"
#import "LJHCollectionCell.h"
#import "LJHHeaderView.h"
#import "LJHImaWorTableCell.h"
#import "LJHListenModel.h"
#import "LJHListModel.h"
#import "LJHDisAllViewController.h"
#import "LJHAlbumsDetailViewController.h"
#import "LJHMusicListViewController.h"
#import "LJHMoreListViewController.h"
#import "LJHCateScrollViewController.h"


@interface LJHRecommendViewController ()<UITableViewDataSource,UITableViewDelegate,PicTableCellDelegate,myHeadScrollViewDelegate>

@property(nonatomic,retain)LJHRecScrollView *headView;
@property(nonatomic,retain)NSMutableArray *arrSection;
@property(nonatomic,retain)NSMutableArray *editorArray;
@property(nonatomic,retain)NSMutableArray *specialArray;
@property(nonatomic,retain)NSMutableArray *disArray;
@property(nonatomic,retain)NSMutableArray *lisArray;
@property(nonatomic,retain)NSMutableArray *hotArray;
@property(nonatomic,retain)NSMutableArray *hotRecommends;
@property(nonatomic,retain)NSMutableArray *focusArray;

@end

@implementation LJHRecommendViewController

- (void)dealloc
{
    
    [_focusArray release];
    [_arrSection release];
    [_hotArray release];
    [_editorArray release];
    [_specialArray release];
    [_headView release];
    [_hotRecommends release];
    [_lisArray release];
    [_disArray release];
    [super dealloc];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"l",@"1",@"j",@"2", nil];
    NSLog(@"%@",dic);
    NSArray *a = dic.allKeys;
    NSLog(@"%@",a);
    
    [GiFHUD setGifWithImageName:@"loading.gif"];//菊花gif加载
    [GiFHUD show];
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];//不让scrollview自适应高度
    
    [self getData];
    
    self.myTableView = [[LJHBaseTableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-111) style:UITableViewStyleGrouped];//创建tableview
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.myTableView registerClass:[LJHCollectionCell class] forCellReuseIdentifier:@"cell"];
    [self.myTableView registerClass:[LJHImaWorTableCell class] forCellReuseIdentifier:@"imaword"];
    [self.myTableView registerClass:[LJHImaWorTableCell class] forCellReuseIdentifier:@"ima"];
    [self.view addSubview:self.myTableView];
    [self.myTableView reloadData];

    self.headView = [[LJHRecScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH*0.45)]; //头部轮播视图
    self.myTableView.tableHeaderView = self.headView;
    self.headView.scrollDelegate = self;
    
    [_myTableView release];
    
    // Do any additional setup after loading the view.
}

#pragma mark - tableview协议方法

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    LJHHeaderView *headView = [[LJHHeaderView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    LJHListenModel *model = [self.arrSection objectAtIndex:section];
    
    headView.cateLable.text = model.title;
    headView.imageMore.tag = section;
    headView.imageMore.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(morePush:)];
    [headView.imageMore addGestureRecognizer:tap];
    
    return headView;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.arrSection.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    LJHListenModel *model = [self.arrSection objectAtIndex:section];

    if ([model.title isEqualToString:@"精品听单"]) {

        return model.list.count;
    }
    if ([model.title isEqualToString:@"发现新奇"]) {
        
        return model.list.count;
        
    }
    
    return 1;
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    LJHCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    LJHListenModel *model = [self.arrSection objectAtIndex:indexPath.section];
    if ([model.title isEqualToString:@"小编推荐"]) {
        cell.allPicArray = self.editorArray;
        cell.myTitle = model.title;
        cell.picTableCellDelegate = self;
        return cell;
    }

    if ([model.title isEqualToString:@"精品听单"]) {
        LJHImaWorTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imaword"];
        cell.model = [self.specialArray objectAtIndex:indexPath.row];
        
        return cell;
    }
    if ([model.title isEqualToString:@"发现新奇"]) {
        LJHImaWorTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ima"];
        cell.model = [self.disArray objectAtIndex:indexPath.row];
        cell.iconImage.alpha = 0;
        return cell;
    }
    cell.allPicArray = [self.hotArray objectAtIndex:indexPath.section-3];
    cell.myTitle = model.title;
    cell.picTableCellDelegate = self;
    
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;

    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; //取消返回置灰
    
    LJHListModel *model = [self.arrSection objectAtIndex:indexPath.section];
    if ([model.title isEqualToString:@"精品听单"]) {
        
        [self.myPush pushMusicVC:((LJHChildListModel *)model.list[indexPath.row]).specialId];
        
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJHListenModel *model = [self.arrSection objectAtIndex:indexPath.section];
    
    if ([model.title isEqualToString:@"精品听单"]) {
        return WIDTH/3;
    }
    if ([model.title isEqualToString:@"发现新奇"]) {
        return WIDTH/3;
    }
    return WIDTH/2.5;
}

#pragma mark - 点击更多btn

-(void)morePush:(UITapGestureRecognizer *)tap //点击更多按钮方法
{
    if (tap.view.tag == 0 || tap.view.tag == 1) {
        
        if (tap.view.tag == 1)
        {
            
            LJHMoreListViewController *moreListVC = [[LJHMoreListViewController alloc] init];
            moreListVC.myTitle = @"精品听单";
            moreListVC.moreUrl = @"http://mobile.ximalaya.com/m/subject_list?device=iphone&page=1&per_page=10&title=更多";
            [(LJHTabBarViewController *)self.tabBarController isHiddenTabBar:YES withViewController:moreListVC];
            [self.myPush pushMoreListVC:moreListVC.myTitle];
            
        }
        if (tap.view.tag == 0) {
            
            LJHMoreListViewController *moreListVC = [[LJHMoreListViewController alloc] init];
            moreListVC.myTitle = @"小编推荐";
            moreListVC.moreUrl = @"http://mobile.ximalaya.com/mobile/discovery/v1/recommend/editor?device=iphone&pageId=1&pageSize=20&title=更多";
            [self.myPush pushMoreListVC:moreListVC.myTitle];
            
        }
    }
    else if (tap.view.tag != 2&&tap.view.tag != 0&&tap.view.tag != 1 )
    {
        
        LJHListModel *model = [self.hotRecommends objectAtIndex:tap.view.tag-3];
        LJHCateScrollViewController *categorySVC = [[LJHCateScrollViewController alloc] init];
        [(LJHTabBarViewController*)self.tabBarController isHiddenTabBar:YES withViewController:categorySVC];
        categorySVC.categoryId = model.categoryId;
        [self.myPush push:model.categoryId];
        
    }
 
}

#pragma mark - 协议方法collection跳转

-(void)JumpToAlbumView:(NSString *)albumId :(NSString *)myTitle :(NSInteger)trackId //点击collectionview跳转页面
{

    [self.myPush pushDetail:albumId :myTitle :trackId];
    
}

#pragma mark - 解析数据

-(void)getData
{
    
    self.focusArray = [NSMutableArray array];
    self.hotArray = [NSMutableArray array];
    self.hotRecommends = [NSMutableArray array];
    NSString *urlStr = @"http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=and-f6&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.3.44.2"; //主页接口
    
    [LJHAppTools afnGetRequest:urlStr block:^(id result) {
        
        NSDictionary *dicResult = result;
        LJHSectionModel *sectionModel = [LJHSectionModel modelWithDictionary:dicResult];//最外层model，解析出数据
        self.editorArray = sectionModel.editorRec.list; //小编推荐
        self.specialArray = sectionModel.special.list; //精品听单
        self.disArray = sectionModel.discoveryColumn.list; //发现新奇
        self.lisArray = sectionModel.lisModel.list; //热门推荐（听系列）
        for (LJHListModel *model in sectionModel.lisModel.list) {

            [self.hotRecommends addObject:model]; //head视图
            [self.hotArray addObject:model.list]; //collectionview数组
            
        }

        
        [self.headView setScrollViewImage:sectionModel.focus.list];
        self.focusArray = sectionModel.focus.list; //轮播图
        
        
        self.arrSection = [NSMutableArray array];
        [self.arrSection addObject:sectionModel.editorRec];
        [self.arrSection addObject:sectionModel.special];
        [self.arrSection addObject:sectionModel.discoveryColumn];
        
        [self.arrSection addObjectsFromArray:sectionModel.lisModel.list];

        [self.myTableView reloadData];
        
        
    } error:^(NSError *error) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"您的设备无网络访问" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        
        UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertC addAction:cancelButton];
        
    }];
    
}

#pragma mark - 轮播图点击方法

-(void)sendMyImageTagBack:(NSInteger)num
{
        LJHChildListModel *model =  [self.focusArray objectAtIndex:num-1];
        //type 2 专辑
        if (model.imageType == 2) {
            
            [self.myPush pushDetail:model.albumId :model.title :model.trackId];
            
        }
        //type 3 音乐
        else if (model.imageType == 3)
        {

            [self.myPush presentPlay:model.trackId arr:[NSMutableArray arrayWithObjects:model, nil]];
            
        }
        //精品听单
        else if (model.imageType == 9)
        {
            
            [self.myPush pushMusicVC:model.specialId];
            
        }
        
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
